package com.ybb.dao;

import com.ybb.pojo.Role;
import com.ybb.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> getAllRole() {
        Connection connection = JdbcUtil.getConnection();
        List<Role> roleList=new ArrayList<Role>();
        if (connection != null) {
            String sql = "select * from smbms_role";
            PreparedStatement preparedStatement = null;
            ResultSet rs=null;

            try {
                preparedStatement = connection.prepareStatement(sql);
                rs= JdbcUtil.query(preparedStatement, new Object[]{});
                while (rs.next()){
                    Role role=new Role();
                    role.setId(rs.getLong("id"));
                    role.setRoleCode(rs.getString("roleCode"));
                    role.setRoleName(rs.getString("roleName"));
                    roleList.add(role);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                   JdbcUtil.close(rs,preparedStatement,connection);
            }

        }
return roleList;
    }
}
